﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;

namespace Mocola.Common
{
    public class SiteUser
    {
        public int UserID;
        public string UserName;
        public string UserPwd;
        public bool UserSex;
        public int Status;
        public int LoginTimes;
        public DateTime LastLoginTime;
        public DateTime LastPwdChangeTime;
        public DateTime LastLockTime;
        public string Email;
        public string FirstName;
        public string LastName;
        public string FullName;
        public int RoleID;
        public string Phone;
        public string RolesName;
        public string Params;

        public SiteUser(SqlDataReader pReader) 
        {
            this.UserID = !pReader.IsDBNull(0) ? pReader.GetInt32(0) : -1;
            this.UserName = !pReader.IsDBNull(1) ? pReader.GetString(1) : string.Empty;
            this.UserPwd = !pReader.IsDBNull(2) ? pReader.GetString(2) : string.Empty;
            this.UserSex = !pReader.IsDBNull(3) ? pReader.GetBoolean(3) : false;
            this.Status = !pReader.IsDBNull(4) ? pReader.GetInt32(4) : 0;
            this.LoginTimes = !pReader.IsDBNull(5) ? pReader.GetInt32(5) : 0;
            this.LastLoginTime = !pReader.IsDBNull(6) ? pReader.GetDateTime(6) : new DateTime();
            this.LastPwdChangeTime = !pReader.IsDBNull(7) ? pReader.GetDateTime(7) : new DateTime();
            this.LastLockTime = !pReader.IsDBNull(8) ? pReader.GetDateTime(8) : new DateTime();
            this.Email = !pReader.IsDBNull(9) ? pReader.GetString(9) : string.Empty;
            this.FirstName = !pReader.IsDBNull(10) ? pReader.GetString(10) : string.Empty;
            this.LastName = !pReader.IsDBNull(11) ? pReader.GetString(11) : string.Empty;
            this.FullName = !pReader.IsDBNull(12) ? pReader.GetString(12) : string.Empty;
            this.RoleID = !pReader.IsDBNull(13) ? pReader.GetInt32(13) : -1;
            this.Phone = !pReader.IsDBNull(14) ? pReader.GetString(14) : string.Empty;
            this.RolesName = !pReader.IsDBNull(15) ? pReader.GetString(15) : string.Empty;
        }

        // 实际应该由数据库获得
        public static SiteUser GetUser(string name)
        {
            if (name.Trim() != string.Empty)
            {
                string SqlString = @"SELECT MU.UserID, 
                                            MU.UserName, 
                                            MU.UserPwd, 
                                            MU.UserSex, 
                                            MU.Status, 
                                            MU.LoginTimes, 
                                            MU.LastLoginTime, 
                                            MU.LastPwdChangeTime, 
                                            MU.LastLockTime, 
                                            MU.Email,
                                            MU.FirstName, 
                                            MU.LastName, 
                                            MU.FullName, 
                                            MU.RoleID, 
                                            MU.Phone, 
                                            MR.RoleName
                                    FROM Moc_Users MU, Moc_Roles MR 
                                    WHERE MU.UserName=@UserName AND MU.RoleID = MR.ID";

                System.Configuration.ConnectionStringSettings connString = WebConfigurationManager.ConnectionStrings["MocolaNet"];
                SqlConnection connection1 = new SqlConnection(connString.ConnectionString);
                SqlCommand command1 = connection1.CreateCommand();
                command1.CommandText = SqlString;
                command1.Parameters.Add("@UserName", SqlDbType.NVarChar);
                command1.Parameters["@UserName"].Value = name;
                connection1.Open();

                SqlDataReader reader1 = command1.ExecuteReader(CommandBehavior.CloseConnection);

                try
                {
                    while (reader1.Read())
                    {
                        return new SiteUser(reader1);
                    }
                }
                finally
                {
                    reader1.Close();
                }
            }

            throw new ApplicationException("User Not Found!");
        }
    }
}
